/*ic_arrow_list_blac
 * @Description: File Description
 * @Author: jundie.cheng@hand-china.com
 * @version: H3.0
 * @Date: 2023-03-28 15:33:13
 * @LastEditTime: 2023-03-28 15:35:29
 */
import type { TTController } from './definition'
import type { Worker } from '@/controller/base/types/worker'
import type { Remote } from '@/controller/base/types/remote'
import { useWorker, useRemote, wrapSafeActions } from '@/controller/base/index'
import { RemoteTypes } from '@/controller/base/remote'
import { createBaseState, BaseGetters, BaseActions, createBaseApplianceControl } from '../BaseAppliance'
import { defineStore } from 'pinia'
import {
  TT_SETTING_BLMODE,
  TT_SETTING_CHSET,
  TT_SETTING_CHWT,
  TT_SETTING_FLAME,
  TT_SETTING_MODE,
  TT_SUMMER_BLMODE,
  TT_SUMMER_MODE,
  TT_WATER_TEMP,
} from '@/constants/Command'
import {
  getRoomValve,
  getTTAutoMode,
  getTTFlameState,
  getTTSummerMode,
  TT_SETTING_BlCHMAX,
  TT_SETTING_BlCHSet,
  TT_SETTING_BlCHWt,
  TT_SETTING_BlDHWMax,
  TT_SETTING_BlDHWSet,
  TT_SETTING_BlDHWWt,
  TT_SETTING_BLFLAME,
  TT_SETTING_BlState,
  TT_STATUS_MCUSTATUS,
} from '@/pages/appliance/TT/SmartAssistantTool'
import type { Binder } from '../base/types/binder'
import type { ApplianceSetting } from '@/api/appliance'
import { actionDebounce } from '@/utils/mpHelper'

/**
 * 初始化数据
 */
export const createTTState: () => TTController.State = () => ({
  ...createBaseState(),
  warmTemp: '0', // 采暖实际值
  warmTempInt: '--',
  warmTempDec: '--',
  warmTempSetInt: '0', // 采暖设置值
  warmTempSetDec: '0',
  waterTemp: '0', // 生活热水实际值
  waterTempInt: '--',
  waterTempDec: '--',
  waterTempSetInt: '0', // 生活热水设置值
  waterTempSetDec: '0',
  flameState: 0, // 火焰
  isShowWarmTemp: true, // 是否显示采暖
  isShowWaterTemp: true, // 是否显示生活热水
  curTab: 1,
  blState: '',
  summerMode: false, // 运行模式 true夏季，false冬季
  BlCHMAX: '', // 采暖设定最大值
  BlDHWMax: '', // 生活热水设定最大值
  mcuStatus: 0,
})
export const TTGetters: TTController.Getters = {
  ...BaseGetters,
  isMR120({ _datasource: { haId } }) {
    return haId.indexOf('MR120') !== -1
  },
  isMR100IRF({ _datasource: { haId } }) {
    return haId.split('-')[1] === 'MR100iRF10720'
  },
  isMR100IRFEMS({ _datasource: { haId } }) {
    return haId.split('-')[1] === 'MR100iRF10720EMS'
  },
}
export const TTActions: TTController.Actions = wrapSafeActions<TTController.Store, TTController.Actions>({
  ...BaseActions,
})

/**
 * 组合API
 */
const useStore = defineStore<'TT', TTController.State, TTController.Getters, TTController.Actions>('TT', {
  state: createTTState,
  getters: TTGetters,
  actions: TTActions,
})

// 组件使用
export function useTTControl(): TTController.Control {
  const store = useStore() as TTController.Store
  const worker = useWorker(store) as Worker.ApplianceWorker<TTController.Store>
  const remote = useRemote(store) as Remote.ApplianceRemote<TTController.Store>
  return { store, worker, remote }
}

// 页面使用
export function createTTControl(control: TTController.Control = useTTControl()): TTController.Control {
  const { store, worker, remote } = control

  const loadData = actionDebounce({
    call: async () => {
      const tasks = []
      if (store.isOffline === false) {
        tasks.push(remote.loadAllSetting())
      }
      return Promise.all(tasks)
    },
  })

  // Init by base control
  createBaseApplianceControl(control, { remoteType: RemoteTypes.Oem })

  useTTBinding(control)
  worker.onLoad(loadData)
  worker.onShow(loadData)

  return control
}

export function useTTBinding(control: TTController.Control) {
  const { store, worker } = control

  // 采暖实际温度
  const warmTempPatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value: warmTempValue } = reference
    let newValue = Number(warmTempValue)
    let newWarmTempInt = '--'
    let newWarmTempDec = '--'
    if (store.isMR120) {
      if (newValue.toString(16) == '8000') {
        patchState.isShowWarmTemp = false
      } else {
        patchState.isShowWarmTemp = true
      }
    } else {
      newWarmTempInt = Math.floor(newValue / 10) + ''
      newWarmTempDec = newValue - 10 * Math.floor(newValue / 10) + ''
      if (store.isMR100IRFEMS && Number(warmTempValue) == 1200) {
        newWarmTempInt = '--'
        newWarmTempDec = '--'
      }
    }
    patchState.warmTempInt = newWarmTempInt
    patchState.warmTempDec = newWarmTempDec
    patchState.warmTemp = warmTempValue as string
  }
  worker.useGetValueBinding('warmTemp', {
    key: TT_SETTING_CHWT,
    from: 'setting',
    patcher: warmTempPatcher,
  })
  worker.useGetValueBinding('warmTemp', {
    key: TT_SETTING_BlCHWt,
    from: 'setting',
    patcher: warmTempPatcher,
  })

  // 采暖设置值
  const warmTempSetIntPatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value: warmTempSetIntValue } = reference
    if (!warmTempSetIntValue) return
    patchState.warmTempSetInt = warmTempSetIntValue as string
    patchState.warmTempSetDec = '0'
  }
  worker.useGetValueBinding('warmTempSetInt', {
    key: TT_SETTING_CHSET,
    from: 'setting',
    patcher: warmTempSetIntPatcher,
  })
  worker.useGetValueBinding('warmTempSetInt', {
    key: TT_SETTING_BlCHSet,
    from: 'setting',
    patcher: warmTempSetIntPatcher,
  })

  // 生活热水实际温度
  const waterTempPatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value: waterTempValue } = reference
    patchState.warmTempSetDec = '0'
    let newValue = Number(waterTempValue)
    let newWaterTempInt = '--'
    let newWaterTempDec = '--'
    if (store.isMR120) {
      if (newValue.toString(16) == '8000') {
        patchState.isShowWaterTemp = false
      } else {
        patchState.isShowWaterTemp = true
      }
    } else {
      newWaterTempInt = Math.floor(newValue / 10) + ''
      newWaterTempDec = newValue - 10 * Math.floor(newValue / 10) + ''
      if (store.isMR100IRFEMS && Number(waterTempValue) == 1200) {
        newWaterTempInt = '--'
        newWaterTempDec = '--'
      }
    }
    patchState.waterTempInt = newWaterTempInt
    patchState.waterTempDec = newWaterTempDec
    patchState.waterTemp = waterTempValue as string
  }
  worker.useGetValueBinding('waterTemp', {
    key: TT_WATER_TEMP,
    from: 'setting',
    patcher: waterTempPatcher,
  })
  worker.useGetValueBinding('waterTemp', {
    key: TT_SETTING_BlDHWWt,
    from: 'setting',
    patcher: waterTempPatcher,
  })

  // 生活热水设置值
  const waterTempSetIntPatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value } = reference
    if (!value) return
    patchState.waterTempSetInt = value as string
    patchState.waterTempSetDec = '0'
  }
  worker.useGetValueBinding('waterTempSetInt', {
    key: TT_SETTING_BlDHWSet,
    from: 'setting',
    patcher: waterTempSetIntPatcher,
  })

  // curTab
  const curTabPatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value } = reference
    patchState.curTab = value ? Number(value) : 0
  }
  worker.useGetValueBinding('curTab', {
    key: TT_SETTING_MODE,
    from: 'setting',
    patcher: curTabPatcher,
  })
  worker.useGetValueBinding('curTab', {
    key: TT_SETTING_BLMODE,
    from: 'setting',
    patcher: curTabPatcher,
  })

  // blState
  const blStatePatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value } = reference
    if (!value) return
    if (getTTSummerMode(value as string) == 1) {
      patchState.summerMode = true
    } else {
      patchState.summerMode = false
    }
    patchState.curTab = getTTAutoMode(value as string)
    patchState.flameState = getTTFlameState(value as string)
    patchState.blState = value as string
  }
  worker.useGetValueBinding('blState', {
    key: TT_SETTING_BlState,
    from: 'setting',
    patcher: blStatePatcher,
  })

  // flameState
  const flameStatePatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value } = reference
    patchState.flameState = Number(value)
  }
  worker.useGetValueBinding('flameState', {
    key: TT_SETTING_FLAME,
    from: 'setting',
    patcher: flameStatePatcher,
  })
  worker.useGetValueBinding('flameState', {
    key: TT_SETTING_BLFLAME,
    from: 'setting',
    patcher: flameStatePatcher,
  })

  // BlCHMAX
  const BlCHMAXPatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value } = reference
    patchState.BlCHMAX = value as string
  }
  worker.useGetValueBinding('BlCHMAX', {
    key: TT_SETTING_BlCHMAX,
    from: 'setting',
    patcher: BlCHMAXPatcher,
  })

  // BlCHMAX
  const BlDHWMaxPatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value } = reference
    patchState.BlDHWMax = value as string
  }
  worker.useGetValueBinding('BlDHWMax', {
    key: TT_SETTING_BlDHWMax,
    from: 'setting',
    patcher: BlDHWMaxPatcher,
  })

  // mcuStatus
  const mcuStatusPatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value } = reference
    patchState.mcuStatus = Number(value)
  }
  worker.useGetValueBinding('mcuStatus', {
    key: TT_STATUS_MCUSTATUS,
    from: 'setting',
    patcher: mcuStatusPatcher,
  })

  // summerMode
  const summerModePatcher: Binder.StatePatcher<TTController.Store, ApplianceSetting> = (patchState, reference) => {
    const { item, value } = reference
    patchState.summerMode = Number(value) === 1
  }
  worker.useGetValueBinding('summerMode', {
    key: TT_SUMMER_MODE,
    from: 'setting',
    patcher: summerModePatcher,
  })
  worker.useGetValueBinding('summerMode', {
    key: TT_SUMMER_BLMODE,
    from: 'setting',
    patcher: summerModePatcher,
  })
}
